home *** CD-ROM | disk | FTP | other *** search
- Path: news.cs.ucla.edu!edwin
- From: edwin@cs.ucla.edu (E. Robert Tisdale)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Overloading >>
- Date: 11 Jan 1996 02:43:42 GMT
- Organization: UCLA Computer Science Dept.
- Message-ID: <4d1tgu$5r0@delphi.cs.ucla.edu>
- References: <4d1f4q$7oj@news.cs.hope.edu>
- NNTP-Posting-Host: flamingo.cs.ucla.edu
- X-Newsreader: NN version 6.5.0.b3.0 #9 (NOV)
-
- vnopstal@cs.hope.edu (Michael Van Opstall) writes:
-
- >I was actually pretty clear on output stream overloading, but I really have
- >a question on overloading the >> operator. I really just need the function
- >header and param list. Right now, I'm using this:
-
- >istream& operator>>(istream& in, typename t1)
-
- >which compiles, but I don't understand what stream is which, nor what to
- >do with t1. Am I supposed to do something like
- >
- >in >> whatever >> I >> want;
- >t1.fpart=whatever;
- >t1.mpart=I;
- >t1.lpart=want;
- >return in;
-
- >or what? Why do I need to return an istream&? Same reason the = op requires
- >a return?
-
- istream& operator>>(istream& in, typename& t1) {
- type_fpart whatever;
- if (in >> whatever) {
- type_mpart I;
- if (in >> I) {
- type_lpart want;
- if (in >> want) {
- t1 = typename(whatever, I, want);
- }
- else {
- cerr << "Error reading lpart!\n";
- }
- }
- else {
- cerr << "Error reading mpart!\n";
- }
- }
- else {
- cerr << "Error reading fpart!\n";
- }
- return in;
- }
-
- 1.) Pass a reference to your input variable t1.
- 2.) Don't alter t1 before you have read all the parts.
- 3.) Return in to stop the compiler from complaining about it.
- It doesn't really matter because in was passed by reference.
-
- Hope this helps, Bob Tisdale.
-